package com.css.zfzx.fxpg.modules.percent.damage.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.zfzx.fxpg.modules.percent.damage.repository.entity.FxDamagePercentEntity;
import com.css.zfzx.fxpg.modules.percent.damage.service.FxDamagePercentService;
import com.css.zfzx.fxpg.modules.percent.damage.viewobjects.FxDamagePercentQueryParams;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.zfzx.fxpg.modules.percent.daynight.viewobjects.FxDayNightPercentQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author leon
 * @date 2020-11-13
 */
@Slf4j
@RestController
@RequestMapping("/fxDamagePercents")
public class FxDamagePercentController {
    @Autowired
    private FxDamagePercentService fxDamagePercentService;

    @GetMapping("/queryFxDamagePercents")
    public RestResponse queryFxDamagePercents(HttpServletRequest request, FxDamagePercentQueryParams queryParams) {
        RestResponse response = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = fxDamagePercentService.queryFxDamagePercents(queryParams, curPage, pageSize);
            response = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getFxDamagePercent(@PathVariable String id) {
        RestResponse response = null;
        try {
            FxDamagePercentEntity fxDamagePercent = fxDamagePercentService.getFxDamagePercent(id);
            response = RestResponse.succeed(fxDamagePercent);
        } catch (Exception e) {
            String errorMessage = "获取失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @PostMapping
    public RestResponse saveFxDamagePercent(@RequestBody FxDamagePercentEntity fxDamagePercent) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            boolean flag = fxDamagePercentService.isRepeat(fxDamagePercent.getStructureType());
            if (flag) {
                json.put("message", "结构类型重复");
            }else {
                fxDamagePercentService.saveFxDamagePercent(fxDamagePercent);
                json.put("message", "新增成功!");
            }
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "新增失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;

    }

    @PutMapping("/cover")
    public RestResponse coverFxDamagePercent(@RequestBody FxDamagePercentEntity fxDamagePercent) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxDamagePercentService.coverFxDamagePercent(fxDamagePercent);
            json.put("message", "覆盖成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "覆盖失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
    @PutMapping
    public RestResponse updateFxDamagePercent(@RequestBody FxDamagePercentEntity fxDamagePercent) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxDamagePercentService.updateFxDamagePercent(fxDamagePercent);
            json.put("message", "修改成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "修改失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @DeleteMapping
    public RestResponse deleteFxDamagePercents(@RequestParam List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try {
            fxDamagePercentService.deleteFxDamagePercents(ids);
            json.put("message", "删除成功!");
            response = RestResponse.succeed(json);
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    @GetMapping("/getValidDictItemsByDictCode/{dictCode}")
    public RestResponse getValidDictItemsByDictCode(@PathVariable String dictCode) {
        RestResponse restResponse = null;
        try {
            restResponse = RestResponse.succeed(fxDamagePercentService.getValidDictItemsByDictCode(dictCode));
        } catch (Exception e) {
            String errorMsg = "字典项获取失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    /**
     * 导出
     *
     * @param queryParams
     * @param response
     * @return
     */
    @GetMapping("/export")
    public RestResponse exportExcel(FxDamagePercentQueryParams queryParams, HttpServletResponse response) {
        RestResponse restResponse = null;
        try {
            String s = fxDamagePercentService.exportExcel(queryParams, response);
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errorMsg = "导出失败!";
            log.error(errorMsg, e);
            restResponse = RestResponse.fail(errorMsg);
        }
        return restResponse;
    }

    @GetMapping("/getFxDamagePercents")
    public RestResponse getFxDamagePercents(HttpServletRequest request, FxDamagePercentQueryParams queryParams) {
        RestResponse response = null;
        try {
            List<FxDamagePercentEntity> fFxDamagePercentEntities = fxDamagePercentService.getFxDamagePercents(queryParams);
            response = RestResponse.succeed(fFxDamagePercentEntities);
        } catch (Exception e) {
            String errorMessage = "查询失败!";
            log.error(errorMessage, e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}